package com.css.fxfzfxqh.modules.reportTemplate.repository.impl;

import com.css.fxfzfxqh.modules.buildings.calculationTasks.entity.CalculationTasks;
import com.css.fxfzfxqh.modules.reportTemplate.entity.Template;
import com.css.fxfzfxqh.modules.reportTemplate.repository.TemplateRepository;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import com.css.fxfzfxqh.web.PlatformSessionContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年10月24日  10:31
 */
@Repository
public class TemplateRepositoryImpl implements TemplateRepository {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void save(Template template) {
        String sql = "insert into  fxqh_report_template(id,num,name,template_desc,create_user_id,create_user_name," +
                "create_time,report_type,input,attach_id) " +
                "values(:id,:num,:name,:templateDesc,:createUserId,:createUserName," +
                ":createTime,:reportType,:input,:attachId)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(template);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void update(String id, String attachId,String name,String templateDesc) {
        String updateUserId = PlatformObjectUtils.isNotEmpty(PlatformSessionContext.getUserID())?PlatformSessionContext.getUserID():"80cd530287e54c6788925adf2dfc9da1";
        String sql = "update fxqh_report_template set update_user_id='"+ updateUserId +"',update_time=now() ";
        if(PlatformObjectUtils.isNotEmpty(name)){
            sql+=",name='"+name+"'";
        }
        if(PlatformObjectUtils.isNotEmpty(templateDesc)){
            sql+=",template_desc='"+templateDesc+"'";
        }
        if(PlatformObjectUtils.isNotEmpty(attachId)){
            sql+=",attach_id='"+attachId+"'";
        }
        sql+=" where id='"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public Template findById(String id) {
        String sql = "select * from fxqh_report_template where id='"+id+"'";
        List<Template> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Template.class));
        return query.get(0);
    }

    @Override
    public void delete(String id) {
        String sql = "delete from fxqh_report_template where id='"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, String num,String name) {
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        StringBuilder sql = new StringBuilder("select * from fxqh_report_template where 1=1  ");
        if (!PlatformObjectUtils.isEmpty(num)) {
            sql.append(" and num like '%" + num + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(name)) {
            sql.append(" and name like '%" + name + "%'");
        }
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(Template.class)).size();
        List<Template> list = jdbcTemplate.query(sql.append(orderSql).append(limitSql).toString(), new BeanPropertyRowMapper<>(Template.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public void deleteByNum(String num) {
        jdbcTemplate.update("delete from fxqh_report_template where num='"+num+"'");
    }

    @Override
    public int getByTemplateNum(String num) {
        String sql = "select count(*) from fxqh_report_template where 1=1 ";
        if(StringUtils.isNotBlank(num)){
            sql+=" and num='"+num+"'";
        }
        Integer result = jdbcTemplate.queryForObject(sql, Integer.class);
        if(PlatformObjectUtils.isEmpty(result)){
            return 0;
        }
        return result;
    }

    @Override
    public Template getByTemplateName(String name) {
        String sql = "select * from fxqh_report_template where 1=1 ";
        if(StringUtils.isNotBlank(name)){
            sql+=" and name='"+name+"'";
        }
        List<Template> template = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Template.class));
        if(PlatformObjectUtils.isEmpty(template)){
            return null;
        }
        return template.get(0);
    }

    @Override
    public Template getByTemplateInput(String input) {
        String sql = "select * from fxqh_report_template where 1=1 ";
        if(StringUtils.isNotBlank(input)){
            sql+=" and input='"+input+"'";
        }
        List<Template> template = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Template.class));
        if(PlatformObjectUtils.isEmpty(template)){
            return null;
        }
        return template.get(0);
    }
}
